package controller

import (
	"github.com/gin-gonic/gin"
	"shop-common/library/serializer"
	"shop-common/library/variables"
	"shop-common/utils"
	"shop-common/utils/contextArgs"
	"shop-common/utils/pagehelper"
	"shop-common/utils/queryWrapper"
	"shop-sys/business"
	"shop-sys/model"
	"shop-sys/pkg/request"
)

type RequestController struct {
}

// @Tags 请求管理
// @Summary 获取请求页面的表
// @Security ApiKeyAuth
// @function: List
// @Success 200 {object} string "{"code":1," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /sys/xhr/list [get]
func (q *RequestController) List(ctx *gin.Context) {
	var all []*model.SysXhr
	err := variables.GormDB().Model(&model.SysXhr{}).Where("status = 2").Find(&all).Error
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	serializer.SuccessData(ctx, "", &all)
}

// @Tags 请求管理
// @description: 分页
// @Summary 分页
// @Security ApiKeyAuth
// @function: Page
// @Accept  json
// @Produce  json
// @Param current query string false "当前页，默认1"
// @Param size query string false "每页大小，默认10"
// @Param keyword query string false "请求路径"
// @Param status query int false "状态"
// @Param category query string false "分组"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /sys/xhr/page [get]
func (q *RequestController) Page(ctx *gin.Context) {
	form := request.SysXhrSearchRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	//pair := &queryWrapper.WherePair{}
	//if form.Keyword != "" {
	//	pair = pair.And(queryWrapper.Like("full_path", form.Keyword))
	//}
	//if form.Category != "" {
	//	pair = pair.And(queryWrapper.Eq("category", form.Category))
	//}
	//if form.Method != "" {
	//	pair = pair.And(queryWrapper.Eq("method", form.Method))
	//}
	//
	//if form.Status > 0 {
	//	pair = pair.And(queryWrapper.Eq("status", form.Status))
	//}

	paginator := pagehelper.New().SetOffset(&form).SetFilter([]string{"id"}).
		IsPush(form.Keyword != "", queryWrapper.Like("full_path", form.Keyword)).
		IsPush(form.Category != "", queryWrapper.Eq("category", form.Category)).
		IsPush(form.Method != "", queryWrapper.Eq("method", form.Method)).
		IsPush(form.Status > 0, queryWrapper.Eq("status", form.Status)).
		Paginator()

	page, err := business.ServiceImpl().SysLogService.Page(paginator)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}
	serializer.SuccessData(ctx, "", page)

	//var rows []*model.SysXhr
	//tx := variables.GormDB().Model(&model.SysXhr{}).Where(pair.Query, pair.Args...)
	//err := tx.Scopes(pagehelper.Paginate(form.Current, form.Size)).Find(&rows).Error
	//if err != nil {
	//	serializer.Fail(ctx, err.Error())
	//	return
	//}
	//serializer.SuccessData(ctx, "", pagehelper.Dynamic(&pagehelper.PageParams{
	//	Db:          variables.GormDB().Model(&model.SysXhr{}).Where(pair.Query, pair.Args...),
	//	Current:     form.Current,
	//	Size:        form.Size,
	//	CountFields: []string{"id"},
	//}).SetRecords(&rows))
}

// @Tags 请求管理
// @Summary 修改请求状态
// @description: 修改请求状态
// @Security ApiKeyAuth
// @function: UpdateStatus
// @Accept  json
// @Produce  json
// @Param data body request.UpdateSysXhrStatusRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /sys/xhr/status [put]
func (q *RequestController) UpdateStatus(ctx *gin.Context) {
	form := request.UpdateSysXhrStatusRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	columns := &model.SysXhr{
		Id:     form.Id,
		Status: form.Status,
	}

	err := variables.GormDB().Updates(columns).Error
	if err != nil {
		serializer.Fail(ctx, "更新请求状态失败")
		return
	}

	serializer.Success(ctx, "")
}

// @Tags 请求管理
// @description: 新增
// @Summary 新增
// @Security ApiKeyAuth
// @function: Save
// @Accept  json
// @Produce  json
// @Param data body request.SysXhrRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /sys/xhr [post]
func (q *RequestController) Save(ctx *gin.Context) {
	form := request.SysXhrRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	xhr := model.SysXhr{}
	utils.StructMap(&form, &xhr)
	xhr.Status = 2
	err := variables.GormDB().Create(&xhr).Error
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}
	serializer.Success(ctx, "")
}

// @Tags 请求管理
// @description: 修改
// @Summary 修改
// @Security ApiKeyAuth
// @function: Update
// @Accept  json
// @Produce  json
// @Param data body request.SysXhrRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /sys/xhr [put]
func (q *RequestController) Update(ctx *gin.Context) {
	form := request.SysXhrRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if form.Id < 1 {
		serializer.Fail(ctx, "id不能为空")
		return
	}

	xhr := model.SysXhr{}
	utils.StructMap(&form, &xhr)
	err := variables.GormDB().Save(&xhr).Error
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	serializer.Success(ctx, "")
}

// @Tags 请求管理
// @description: 删除
// @Summary 删除
// @Security ApiKeyAuth
// @function: Delete
// @Accept  json
// @Produce  json
// @Param id path int true "id"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /sys/xhr [delete]
func (q *RequestController) Delete(ctx *gin.Context) {
	id, err := contextArgs.GetUriIntPath(ctx, "id")
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	err = variables.GormDB().Where("id = ?", id).Delete(&model.SysXhr{}).Error
	if err != nil {
		serializer.Fail(ctx, "删除失败")
		return
	}

	serializer.Success(ctx, "删除成功")
}
